<?php
namespace App\Http\Controllers\Admin;

use App\Services\Admin\ShopService;
use Illuminate\Http\Request;

class ShopController extends BaseController
{
    public function __construct(ShopService $shopService)
    {
        $this->db = $shopService;
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {;
        $pages['start'] = $request->post('pageStart', 0);
        $pages['limit'] = $request->post('pageLimit', 10);
        $rst = $this->db->getIndex($pages);
        if ($rst['code']!=0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data'], $rst['total']);
    }

    /**
     * 列表
     */
    public function getOne(Request $request)
    {;
        $id = $request->post('id', 0);
        $rst = $this->db->getOne($id);
        if ($rst['code']!=0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data']);
    }


    /**
     * 修改门店
     */
    public function modify(Request $request)
    {;
        //验证参数
        $this->validate($request, [
            'sname' => 'required|min:2|max:50',
            'sdesc' => 'max:255',
        ], [
            'sname.required' => '门店名称必填',
            'sname.min' => '门店名称不能少于 :min 个字符',
            'sname.max' => '门店名称不能大于 :min 个字符',
            'sdesc.max' => '门店描述不能大于 :max 个字符',
        ]);
        $id = $request->post('id', 0);
        $pas['shop_name'] = $request->post('sname', '');
        $pas['describe'] = $request->post('sdesc', '');
        $pas['headimg'] = $request->post('img', '');
        if ($id) {
            $res = $this->db->modify($id, $pas);
        } else {
            $res = $this->db->add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }


    /**
     * 删除
     */
    public function del(Request $request)
    {;
        //验证参数
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'ID必填',
        ]);
        $id = $request->post('id', 0);
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}